package com.ms.content.controller;

import java.util.Arrays;
import java.util.Map;
import java.util.UUID;

import com.ms.common.utils.PageUtils;
import com.ms.common.utils.R;
import com.ms.content.config.JacksonConfig;
import com.ms.content.entity.BargainingRecordEntity;
import com.ms.content.entity.GoodsEntity;
import com.ms.content.util.OssUtils;
import com.ms.content.util.SnowflakeIdWorker;
import com.ms.content.config.TextER;
import com.ms.content.vo.kVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import com.ms.content.entity.BargainActivityEntity;
import com.ms.content.service.BargainActivityService;
import sun.net.www.content.image.png;

import javax.servlet.http.HttpServletRequest;


/**
 * 
 *
 * @author wc
 * @email sunlightcs@gmail.com
 * @date 2022-03-09 18:59:58
 */
@RestController
@RequestMapping("content/bargainactivity")
public class BargainActivityController {
    @Autowired
    private BargainActivityService bargainActivityService;

    @Autowired
    private OssUtils ossUtils;

    @Autowired
    private JacksonConfig jacksonConfig;

    @Autowired
    private TextER textER;

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = bargainActivityService.queryPage(params);
        return R.ok().put("page", page);
    }

    //测试token
    @RequestMapping("/listaa")
    public void re(HttpServletRequest request){
        String token = request.getHeader("token");
        System.out.println(token);
    }





    /**
     * 1. erweima 二维码 根据商品信息生成
     * 2. 当前用户砍价一次  添加到用户
     */
    @PostMapping("/erweima")
    public R erweima(@RequestBody kVo kvo){
            System.out.println(kvo);
            //获取vo中的商品信息 并生成二维码
            GoodsEntity goodsEntity = new GoodsEntity();
            BeanUtils.copyProperties(kvo, goodsEntity);
            String s = goodsEntity.toString();
            textER.encode(s,500,500,"E:\\资料coreJava\\1906a\\miaosha\\img\\"+goodsEntity.getId()+".png");
            System.out.println(goodsEntity.getId()+".png");
            //获取vo中的用户信息 以及商品id 进行一次砍价活动
            bargainActivityService.savedd(kvo);
            return R.ok().put("url","E:\\资料coreJava\\1906a\\miaosha\\img\\"+goodsEntity.getId()+".png");

    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
		BargainActivityEntity bargainActivity = bargainActivityService.getById(id);

        return R.ok().put("bargainActivity", bargainActivity);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody BargainActivityEntity bargainActivity){
        String snowflakeId = SnowflakeIdWorker.getSnowflakeId();
        System.out.println(snowflakeId);

        bargainActivity.setId(Long.valueOf(snowflakeId));
        System.out.println(bargainActivity);
        bargainActivityService.savesj(bargainActivity);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody BargainActivityEntity bargainActivity){
		bargainActivityService.updateById(bargainActivity);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
		bargainActivityService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
